using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data;
using DTO;

using System.Data;

namespace Business
{
    public class PhongService
    {




		//////////////////////////////////////////////////////////

 		                                                         

		//            Enter code your here                      //
        public static PhongDTO LayPhongTheoTen(string TenPhong)
        {
            PhongRepository Repo = new PhongRepository();
            return Repo.LayPhongTheoTen(TenPhong);
        }

        public static bool KiemTraTenTrung(int MaPhong, string TenPhong)
        {
            PhongRepository Repo = new PhongRepository();
            return Repo.KiemTraTenTrung(MaPhong, TenPhong);
        }

        public static List<PhongDTO> LayDanhSachPhongDaThue()
        {
            List<PhongDTO> lst = new List<PhongDTO>();
            try
            {
                PhongRepository obj = new PhongRepository();
                lst = obj.LayDanhSachPhongDaThue();
            }
            catch (System.Exception)
            {
            }
            return lst;
        }

        public static List<PhongDTO> LayDanhSachPhongDaThueTuList(List<PhongDTO> Lst)
        {
            int MaDaThue = TinhTrangDTO.MaDaThue;
            //tu 1 danh sach cac phong, ham se loc ra nhung phong chua duoc thue va tra ve dang list
            List<PhongDTO> newList = new List<PhongDTO>();

            for (int i = 0; i < Lst.Count; i++)
            {
                if (Lst[i].MaTinhTrang == MaDaThue)
                {
                    newList.Add(Lst[i]);
                }
            }
            return newList;
        }
        public static List<PhongDTO> LayDanhSachPhongDaThueTuList(List<PhongDTO> Lst, int MaLoaiPhong)
        {
            //tu 1 danh sach cac phong, ham se loc ra nhung phong chua duoc thue cua loai phong va tra ve dang list
            List<PhongDTO> NewList = new List<PhongDTO>();

            for (int i = 0; i < Lst.Count; i++)
            {
                if (Lst[i].MaLoaiPhong == MaLoaiPhong)
                {
                    NewList.Add(Lst[i]);
                }
            }
            return Business.PhongService.LayDanhSachPhongDaThueTuList(NewList);
        }

        public static List<PhongDTO> LayDanhSachPhongChuaThueTuList(List<PhongDTO> Lst)
        {
            //tu 1 danh sach cac phong, ham se loc ra nhung phong chua duoc thue va tra ve dang list
            List<PhongDTO> newList = new List<PhongDTO>();

            int MaChuaThue = TinhTrangDTO.MaChuaThue;
            for (int i = 0; i < Lst.Count; i++)
            {
                if (Lst[i].MaTinhTrang == MaChuaThue)
                {
                    newList.Add(Lst[i]);
                }
            }
            return newList;
        }
        public static List<PhongDTO> LayDanhSachPhongChuaThueTuList(List<PhongDTO> Lst, int MaLoaiPhong)
        {
            //tu 1 danh sach cac phong, ham se loc ra nhung phong chua duoc thue cua loai phong va tra ve dang list
            List<PhongDTO> NewList = new List<PhongDTO>();

            for (int i = 0; i < Lst.Count; i++)
            {
                if (Lst[i].MaLoaiPhong == MaLoaiPhong)
                {
                    NewList.Add(Lst[i]);
                }
            }
            return Business.PhongService.LayDanhSachPhongChuaThueTuList(NewList);
        }



        public static List<PhongDTO> LayDanhSachPhongTheoLoai(int iMaLoaiPhong)
        {
            Data.PhongRepository Repo = new PhongRepository();
            return Repo.LayDanhSachPhongTheoLoai(iMaLoaiPhong);
        }


        public static int KiemTraMaPhongTrongListPhong(int MaPhong, List<PhongDTO> Lst)
        {
            //tra ve -1 neu khong tim thay, tra ve vi tri phong neu tim thay
            for (int i = 0; i < Lst.Count; i++)
            {
                if (MaPhong == Lst[i].MaPhong)
                {
                    return i;
                }
            }
            return -1;
        }

        public static int TimViTriPhongTrongListPhong(int MaPhong, List<PhongDTO> Lst)
        {
            for (int i = 0; i < Lst.Count; i++)
            {
                if (MaPhong == Lst[i].MaPhong)
                {
                    return i;
                }
            }
            return -1;//khong tim thay
        }                              

		                                                         

		//////////////////////////////////////////////////////////

       static public DataTable ConvertListPhongDTOToDataTable( List<PhongDTO> lst)
       {
               DataTable dt = new DataTable();
           dt.Columns.Add( "MaPhong");
           dt.Columns.Add( "TenPhong");
           dt.Columns.Add( "MaLoaiPhong");
           dt.Columns.Add( "GhiChu");
           dt.Columns.Add( "MaTinhTrang");
           for( int i = 0; i < lst.Count; i ++)
           {
               dt.Rows.Add(
                       lst[i].MaPhong,      
                       lst[i].TenPhong,      
                       lst[i].MaLoaiPhong,      
                       lst[i].GhiChu,      
                       lst[i].MaTinhTrang      
                   );
           }
               return dt;
       }
        static public int GetIdentity()
        {
            Data.PhongRepository repo = new PhongRepository();
            return repo.GetIdentity();
        }

        static public int Count()
        {
            return PhongRepository.Count();
        }
        public static bool Them(PhongDTO objDTO)
        {
                PhongRepository repo = new PhongRepository();
                return repo.Them(objDTO);
        }

        public static bool Xoa(int MaPhong, bool XoaKhoaNgoai=false)
        {
              PhongRepository Repo = new PhongRepository();
              return Repo.Xoa(MaPhong,XoaKhoaNgoai);
        }

        public static void CapNhat(PhongDTO objDTO)
        {
            try
            {
                PhongRepository Repo = new PhongRepository();
                Repo.CapNhat(objDTO);
            }
            catch (System.Exception )
            {
            }
        }

        public static List<PhongDTO> LayDanhSach()
        {
            List<PhongDTO> lst = new List<PhongDTO>();
            try
            {
                PhongRepository obj = new PhongRepository();
                lst = obj.LayDanhSach();
            }
            catch (System.Exception )
            {
            }
            return lst;
        }

        static public List<PhongDTO> LayDanhSach(int Start, int Record)
        {
             Data.PhongRepository repo = new PhongRepository();
            return repo.LayDanhSach(Start, Record);
        }

        public static PhongDTO LayTheoMa(int MaPhong)
        {
            PhongDTO objDTO = new PhongDTO();
            try
            {
                PhongRepository Repo = new PhongRepository();
                objDTO = Repo.LayTheoMa(MaPhong);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai TinhTrang
//
        static public List<PhongDTO> LayDanhSachTheoMaTinhTrang( int MaTinhTrang)
        {
             Data.PhongRepository repo = new PhongRepository();
            return repo.LayDanhSachTheoMaTinhTrang( MaTinhTrang);
        }

        static public void XoaTheoMaTinhTrang( int MaTinhTrang, bool XoaKhoaNgoai=false)
        {
             Data.PhongRepository repo = new PhongRepository();
            repo.XoaTheoMaTinhTrang( MaTinhTrang,XoaKhoaNgoai);
        }


//
//Khoa ngoai LoaiPhong
//
        static public List<PhongDTO> LayDanhSachTheoMaLoaiPhong( int MaLoaiPhong)
        {
             Data.PhongRepository repo = new PhongRepository();
            return repo.LayDanhSachTheoMaLoaiPhong( MaLoaiPhong);
        }

        static public void XoaTheoMaLoaiPhong( int MaLoaiPhong, bool XoaKhoaNgoai=false)
        {
             Data.PhongRepository repo = new PhongRepository();
            repo.XoaTheoMaLoaiPhong( MaLoaiPhong,XoaKhoaNgoai);
        }

    }
}

